<?php
error_reporting(7);
function iserializer($value) {
	return serialize($value);
}


function iunserializer($value) {
	if (empty($value)) {
		return '';
	}
	if (!is_serialized($value)) {
		return $value;
	}
	$result = unserialize($value);
	if ($result === false) {
		$temp = preg_replace_callback('!s:(\d+):"(.*?)";!s', function ($matchs){
			return 's:'.strlen($matchs[2]).':"'.$matchs[2].'";';
		}, $value);
		return unserialize($temp);
	}
	return $result;
}


function is_base64($str){
	if(!is_string($str)){
		return false;
	}
	return $str == base64_encode(base64_decode($str));
}


function is_serialized($data, $strict = true) {
	if (!is_string($data)) {
		return false;
	}
	$data = trim($data);
	if ('N;' == $data) {
		return true;
	}
	if (strlen($data) < 4) {
		return false;
	}
	if (':' !== $data[1]) {
		return false;
	}
	if ($strict) {
		$lastc = substr($data, -1);
		if (';' !== $lastc && '}' !== $lastc) {
			return false;
		}
	} else {
		$semicolon = strpos($data, ';');
		$brace = strpos($data, '}');
				if (false === $semicolon && false === $brace)
			return false;
				if (false !== $semicolon && $semicolon < 3)
			return false;
		if (false !== $brace && $brace < 4)
			return false;
	}
	$token = $data[0];
	switch ($token) {
		case 's' :
			if ($strict) {
				if ('"' !== substr($data, -2, 1)) {
					return false;
				}
			} elseif (false === strpos($data, '"')) {
				return false;
			}
				case 'a' :
		case 'O' :
			return (bool)preg_match("/^{$token}:[0-9]+:/s", $data);
		case 'b' :
		case 'i' :
		case 'd' :
			$end = $strict ? '$' : '';
			return (bool)preg_match("/^{$token}:[0-9.E-]+;$end/", $data);
	}
	return false;
}

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>JSON代码格式、URL解码编码、在线二维码-开发工具</title>
<meta name="description" content="JSON代码格式、URL解码编码-开发工具">
<meta name="keywords" content="JSON代码格式 URL解码编码 JSON转译 在线二维码 二维码">
<link rel="stylesheet" href="bootstrap.min.css">
<script src="jquery.js"></script>
<script src="bootstrap.min.js"></script>
<script src="url_en-decode.js"></script>
<script>
	window.TAB = "  ";
	function IsArray(obj) {
		return obj && typeof obj === 'object' && typeof obj.length === 'number' && !(obj.propertyIsEnumerable('length'))
	}

	function Process() {
		var json = $("#RawJson").val();
		$("#Canvas").css("display","block"); 
		var html = "";
		try {
			if (json == "") json = "\"\"";
			var obj = eval("[" + json + "]");
			html = ProcessObject(obj[0], 0, false, false, false);
			$("#Canvas").html("<PRE class='CodeContainer'>" + html + "</PRE>");
		} catch(e) {
			alert("JSON语法错误,不能格式化,错误信息:\n" + e.message);
			$("#Canvas").html();
		}
	}
	function ProcessObject(obj, indent, addComma, isArray, isPropertyContent) {
		var html = "";
		var comma = (addComma) ? "<span class='Comma'>,</span> ": "";
		var type = typeof obj;
		if (IsArray(obj)) {
			if (obj.length == 0) {
				html += GetRow(indent, "<span class='ArrayBrace'>[ ]</span>" + comma, isPropertyContent)
			} else {
				html += GetRow(indent, "<span class='ArrayBrace'>[</span>", isPropertyContent);
				for (var i = 0; i < obj.length; i++) {
					html += ProcessObject(obj[i], indent + 1, i < (obj.length - 1), true, false)
				}
				html += GetRow(indent, "<span class='ArrayBrace'>]</span>" + comma)
			}
		} else if (type == 'object' && obj == null) {
			html += FormatLiteral("null", "", comma, indent, isArray, "Null")
		} else if (type == 'object') {
			var numProps = 0;
			for (var prop in obj) numProps++;
			if (numProps == 0) {
				html += GetRow(indent, "<span class='ObjectBrace'>{ }</span>" + comma, isPropertyContent)
			} else {
				html += GetRow(indent, "<span class='ObjectBrace'>{</span>", isPropertyContent);
				var j = 0;
				for (var prop in obj) {
					html += GetRow(indent + 1, '<span class="PropertyName">"' + prop + '"</span>: ' + ProcessObject(obj[prop], indent + 1, ++j < numProps, false, true))
				}
				html += GetRow(indent, "<span class='ObjectBrace'>}</span>" + comma)
			}
		} else if (type == 'number') {
			html += FormatLiteral(obj, "", comma, indent, isArray, "Number")
		} else if (type == 'boolean') {
			html += FormatLiteral(obj, "", comma, indent, isArray, "Boolean")
		} else if (type == 'function') {
			obj = FormatFunction(indent, obj);
			html += FormatLiteral(obj, "", comma, indent, isArray, "Function")
		} else if (type == 'undefined') {
			html += FormatLiteral("undefined", "", comma, indent, isArray, "Null")
		} else {
			html += FormatLiteral(obj, "\"", comma, indent, isArray, "String")
		}
		return html
	}
	function FormatLiteral(literal, quote, comma, indent, isArray, style) {
		if (typeof literal == 'string') literal = literal.split("<").join("&lt;").split(">").join("&gt;");
		var str = "<span class='" + style + "'>" + quote + literal + quote + comma + "</span>";
		if (isArray) str = GetRow(indent, str);
		return str
	}
	function FormatFunction(indent, obj) {
		var tabs = "";
		for (var i = 0; i < indent; i++) tabs += window.TAB;
		var funcStrArray = obj.toString().split("\n");
		var str = "";
		for (var i = 0; i < funcStrArray.length; i++) {
			str += ((i == 0) ? "": tabs) + funcStrArray[i] + "\n"
		}
		return str
	}
	function GetRow(indent, data, isPropertyContent) {
		var tabs = "";
		for (var i = 0; i < indent && !isPropertyContent; i++) tabs += window.TAB;
		if (data != null && data.length > 0 && data.charAt(data.length - 1) != "\n") data = data + "\n";
		return tabs + data
	}
</script>
<style>
.Canvas{ font: 10pt Georgia; background-color:#ECECEC; color:#000000; border:solid 1px #CECECE;width:800px;margin:0 auto;overflow:scroll;}
.ObjectBrace{ color:#00AA00; font-weight:bold;}
.ArrayBrace{ color:#0033FF; font-weight:bold;}
.PropertyName{ color:#CC0000; font-weight:bold;}
.String{ color:#007777;}
.Number{ color:#AA00AA;}
.Boolean{  color:#0000FF;}
.Function{  color:#AA6633;  text-decoration:italic;}
.Null{  color:#0000FF;}
.Comma{ color:#000000;  font-weight:bold;}
PRE.CodeContainer{ margin-top:0px;  margin-bottom:0px;}

.toolsTab{margin:0 auto;margin-top:20px;}
.tab{display:none;}
#clipboard_container{float:left; width:100px; height:32px; margin-left:-101px;}
#result_text{float:left; height:32px; line-height:32px; margin-left:5px; color:#f00;}
.hide{display:none;}
.show{display:block;}

.qr-box{height:200px;margin:0 auto;padding:10px;}
.nav{margin-bottom: 10px;}
.space{margin-top:10px;margin-bottom:10px;}
</style>
</head>
<body>
<div class="container" id="container">
<div class="row">
  <div class="col-sm-12">
	<div class="toolsTab clearfix">
		<ul class="nav nav-tabs ">
			<li class="active"><a href="javascript:;;">JSON格式化</a></li>
			<li><a href="javascript:;;">URL编码解码</a></li>
			<li><a href="javascript:;;">生成二维码</a></li>
			<li><a href="javascript:;;">JSON转译</a></li>						 
		</ul>
	</div>
 </div>
 </div>


<div class="row tab show"><div class="col-sm-12">  
<form method='post'>
  <div class="panel panel-default">
  <div class="panel-heading">
    <h3 class="panel-title">JSON代码格式化</h3>
  </div>
    <?php $data=isset($_REQUEST['RawSeri'])?$_REQUEST['RawSeri']:'';?>

  <div class="panel-body">
        <div class="col-xs-9">

    	      <div class="col-xs-9"><textarea id="RawSeri" name="RawSeri"  class="form-control" rows="5" >
			  <?php echo $data;?>
			  
			  </textarea></div>
		</div>
		 <div class="col-xs-3">
		    
         </div>
       
		 <div class="col-xs-12 space">
		   <input type="Button" class="btn btn-default" value="解密" onclick="this.form.submit()">
		 </div>

         <div class="col-xs-9"><textarea id="RawJson" name="RawJson" class="form-control" rows="5" >
		  <?php 
		   if($data){
		      echo json_encode(iunserializer($data));
		   }		  
		  ?>
		 </textarea></div>
		 <div class="col-xs-3">
		    <input type="Button" class="btn btn-default" value="格式化JSON" onclick="Process()" id="ProcessBtn">	
		</div>

		<div id="Canvas" class="col-xs-12"></div>
	 
  </div>
</div>
</form>
</div></div>


 
 <div class="row tab"><div class="col-sm-12">  
  <div class="panel panel-default">

  <div class="panel-heading">
    <h3 class="panel-title">URL编码解码</h3>
  </div>
  <div class="panel-body">
   
		<div class="col-xs-12"><textarea id="code" class="form-control" rows="5" onmouseover="javascript:this.focus();this.select();">请把你需要编码解码的内容粘贴在这里！</textarea></div>
		<div class="col-xs-12 space">		
			<button id="js_escape" class="btn btn-default">escape 编码</button>
			<button id="gb2312_encode" class="btn btn-default">GB2312编码</button>
			<button id="utf8_encode" class="btn btn-default">UTF-8编码</button>
			<button id="decode" class="btn btn-default" >escape/GB2312/UTF-8解码</button>		
		</div>

		<div class="col-xs-12"><textarea class="form-control" rows="5" id="result_code" onmouseover="javascript:this.focus();this.select();"></textarea></div>
		<div class="col-xs-12 space"><button id="copy" class="btn btn-default">复制结果</button><!--[if !IE]><!--><div id="clipboard_container"><embed id="clipboardswf" name="clipboardswf" devicefont="false" width="100" height="32" src="clipboard.swf" wmode="transparent" menu="false" allowscriptaccess="sameDomain" swliveconnect="true" type="application/x-shockwave-flash"></embed></div><!--<![endif]--><button id="clear" class="btn btn-default">清除</button></div>
		<div class="col-xs-12">	
		   <div id="result_text"></div>
      </div>
 
  </div>

</div>
</div></div>



 <div class="row tab"><div class="col-sm-12">  
  <div class="panel panel-default">

  <div class="panel-heading">
    <h3 class="panel-title">生成二维码</h3>
  </div>
  <div class="panel-body">
	<div  class="col-xs-9"><input type="text" class="form-control" id="qrUrl" value=""></div>
    <div  class="col-xs-3"><input class="btn btn-default" type="Button" value="生成二维码" onclick="ProcessUrlQr()"></div>
	<div class="qr-box col-xs-12"><img id='qrcodeimg' src=""></div>
  </div>

</div>
</div></div>

 

  <div class="row tab"><div class="col-sm-12">  
  <div class="panel panel-default">

  <div class="panel-heading">
    <h3 class="panel-title">JSON转译</h3>
  </div>
  <div class="panel-body">

               
				   <div class="col-xs-11">
			        <input type="hidden" id="reformat" value="1" />
                    <input type="hidden" id="compress" value="0" />
                    <textarea id="json_input" name="json_input"  class="form-control" rows="5" spellcheck="false" placeholder="Enter JSON to validate"></textarea>
                   </div>
			 
 
				   <div class="col-xs-11 space">
					<div class="btn-group">
						<div class="left">
						<input type="button"  class="btn btn-default" value="压缩" onclick="yasuo(1);"/>
						<input type="button"  class="btn btn-default" value="转义" onclick="yasuo(2);"/>
						<input type="button"  class="btn btn-default" value="压缩并转义" onclick="yasuo(3);"/>
						<input type="button"  class="btn btn-default" value="去除转义" onclick="yasuo(4);"/>
						<input type="button"  class="btn btn-default" value="Unicode转中文" onclick="u2h()"/>
						<input type="button"  class="btn btn-default" value="中文转Unicode" onclick="h2u()"/>
						<input type="button"  class="btn btn-default" value="中文符号转英文符号" title="如果您从他人技术博客copy代码时,可能会因为json中重要符号被替换成中文字符而导致校验失败,这时就可以使用本功能替换"	onclick="cnChar2EnChar()"/>
						</div>
					</div>
                  </div>
			 

           
  </div>

</div>
</div></div>

</div><!--- container -->
<script>
var ProcessUrlQr=function(){
    var toUrl=$('#qrUrl').val();
    var imgurl="qrcode.php?tourl="+toUrl+'&level=5';
    $('#qrcodeimg').attr('src',imgurl);
}

var ProcessUrlJson=function(){
var toUrl=$('#jsonUrl').val();
var re=/http:\/\/192/;
if(re.test(toUrl))
{
     var url="http://192.168.99.99/getJson.php?url="+encodeURIComponent($('#jsonUrl').val());
}else{
     var url="getJson.php?url="+encodeURIComponent($('#jsonUrl').val());
 }

url="getJson.php?url="+encodeURIComponent($('#jsonUrl').val());
$.ajax({type:'get',url:url,dataType:'jsonp',async:false,jsonp: "jsonpCallback",jsonpCallback:"jsonpCallback",success:function(data){
    $("#RawJson").html(JSON.stringify(data));
    $("#ProcessBtn").click();
  },error:function(data){alert('错误');}});
}
$(".nav-tabs li").click(function(){
	   _this=$(this);
	   _this.parent().find("li.active").removeClass("active");
       _this.addClass("active");
       $("#container .tab.show").removeClass("show");
	   var idx=_this.index();
	   $("#container .tab").eq(idx).addClass("show");
});

</script>
<script>
	/**
	1 压缩
	2 转义
	3 压缩转义
	*/
	function yasuo(ii){
		 var txtA = document.getElementById("json_input");
		 var text = txtA.value;
			if(ii==1||ii==3){
				 text = text.split("\n").join(" ");
				var t = [];
				var inString = false;
				for (var i = 0, len = text.length; i < len; i++) {
					var c = text.charAt(i);
					if (inString && c === inString) {
						// TODO: \\"
						if (text.charAt(i - 1) !== '\\') {
							inString = false;
						}
					} else if (!inString && (c === '"' || c === "'")) {
						inString = c;
					} else if (!inString && (c === ' ' || c === "\t")) {
						c = '';
					}
					t.push(c);
				}
				text= t.join('');
			}
			if(ii==2||ii==3){
				 text = text.replace(/\\/g,"\\\\").replace(/\"/g,"\\\"");
			}
			if(ii==4){
			 text = text.replace(/\\\\/g,"\\").replace(/\\\"/g,'\"');
			}
			 txtA.value = text;
	}	
String.prototype.trim=function()
{
     return this.replace(/(^\s*)|(\s*$)/g, '');
}

var GB2312UnicodeConverter={
  ToUnicode:function(str){
    var txt= escape(str).toLocaleLowerCase().replace(/%u/gi,'\\u');
	return txt.replace(/%7b/gi,'{').replace(/%7d/gi,'}').replace(/%3a/gi,':').replace(/%2c/gi,',').replace(/%27/gi,'\'').replace(/%22/gi,'"').replace(/%5b/gi,'[').replace(/%5d/gi,']');
  }
  ,ToGB2312:function(str){
    return unescape(str.replace(/\\u/gi,'%u'));
  }
};

function u2h(){
	 var txtA = document.getElementById("json_input");
	 var text = txtA.value;
	 text = text.trim();
	// text = text.replace(/\u/g,"");
	 txtA.value = GB2312UnicodeConverter.ToGB2312(text);	 
}

function h2u(){
	var txtA = document.getElementById("json_input");
	 var text = txtA.value;
	 text = text.trim();
	// text = text.replace(/\u/g,"");
	 txtA.value = GB2312UnicodeConverter.ToUnicode(text);
}

function cnChar2EnChar(){
 var txtA = document.getElementById("json_input");
  var str = txtA.value;
str = str.replace(/\’|\‘/g,"'").replace(/\“|\”/g,"\"");
str = str.replace(/\【/g,"[").replace(/\】/g,"]").replace(/\｛/g,"{").replace(/\｝/g,"}");
str = str.replace(/，/g,",").replace(/：/g,":");
 txtA.value = str;
}
</script>
</body>
</html>